<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="username">
                            <Input v-model="form.username" placeholder="请输入用户名">
                                <Icon type="md-person" slot="prepend"/>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <Icon type="md-lock" slot="prepend"></Icon>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                </div>
            </Card>
        </div>
    </div>

</template>

<script>
import Cookies from 'js-cookie'
import { login, getMenus } from '@/api/user'
// import { appRouter } from '@/router/routers'
export default {
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      },
      userMenuList: []
    }
  },
  methods: {
    async userLogin () {
      let res = await login(this.form)
      if (res.code === 200) {
        Cookies.set('token', res.token, {'expires': res.expire / (3600 * 24)})
        let menus = getMenus()
        console.log(menus)
        // this.userMenuList = res.data.menuList;
        // Cookies.set("user", this.form.userAccount);
        // Cookies.set("password", this.form.userPassword);
        // Cookies.set("access", 1);
        // Cookies.set("sessionId", res.data.sessionId);
        // this.$store.commit("initSession");
        // this.$store.commit(
        //   "setAvator",
        //   "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg"
        // )
        // this.setMenu();
        this.$router.push({
          name: 'home'
        })
      } else {
        this.$Message.info(res.msg)
      }
    },
    // setMenu() {
    //   let listObj = {};
    //   searchMenu(this.userMenuList);
    //   walkRouter(appRouter);
    //   Cookies.set("userMenuList", JSON.stringify(listObj));
    //   this.$store.commit("updateMenulist");
    //   function searchMenu(list) {
    //     list.forEach(item => {
    //       listObj[item.menuId] = true;
    //       if (item.childNode) {
    //         searchMenu(item.childNode);
    //       }
    //     });
    //   }
    //   function walkRouter(list) {
    //     list.forEach(item => {
    //       if (!listObj[item.menuId]) {
    //         item.access = 0;
    //       } else {
    //         item.access = 1;
    //       }
    //       if (item.children) {
    //         walkRouter(item.children);
    //       }
    //     });
    //   }
    // },
    async handleSubmit () {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.userLogin()
        }
      })
    }
  }
}
</script>

<style>

</style>
